﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Linq
Imports Exp.Model
Imports Exp.DATA_Service

Public Class ApproveJoin
    Inherits BasePage

    Const MENUNAME As String = "APPRJOIN"

    Const SaveSuccess As String = "Save completed."
    Const SaveError As String = "Save error !"
    Const DeleteSuccess As String = "Delete completed."
    Const DeleteError As String = "Delete not complete"

    Private _User As REM_User

    Private Property user() As REM_User
        Get
            If Session("USER") Is Nothing Then Response.Redirect("~/login.aspx")
            Return Session("USER")
        End Get
        Set(value As REM_User)
            Session("USER") = value
        End Set
    End Property

    Private ds As List(Of EXP_Get_Join_Request_Result)
    Public Property DataSource() As List(Of EXP_Get_Join_Request_Result)
        Get
            Return ds
        End Get
        Set(ByVal value As List(Of EXP_Get_Join_Request_Result))
            ds = value
        End Set
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                user = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(user.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

                MultiView1.ActiveViewIndex = 0

                LoadData()
                BindControl()

            End If
        End If

    End Sub

    Private Sub LoadData()
        Dim ctx As New User_RequestJoin_Service
        DataSource = ctx.Search(Nothing, txtRequestor.Text, GetDataUtil.GetNullableDateTime(txtCheckinDateView0.Text), _
                                GetDataUtil.GetNullableDateTime(txtCheckoutDateView0.Text), GetDataUtil.GetNullableString(ddl_StatusSearch.Text)).ToList()
    End Sub

    Private Sub BindControl()
        grdView0.DataSource = DataSource
        grdView0.DataBind()
    End Sub

    Protected Sub btnSave_Click(sender As Object, e As EventArgs)
        Dim ctx As New User_RequestJoin_Service()
        Try
            ctx.UpdateStatus(user.User_ID, Convert.ToInt32(hdfReqID.Value), ddlStatus.SelectedValue.ToUpper, txtRejectReason.Text, GetDataUtil.GetNullableDateTime(txtJoinDateView1.Text))
            JavaScript.AlertMessage(Me.Page, SaveSuccess)
        Catch ex As Exception
            JavaScript.AlertMessage(Me.Page, SaveError & " : " & ex.Message)
        End Try

        LoadData()
        BindControl()

        MultiView1.ActiveViewIndex = 0
    End Sub

    Protected Sub btnCamcel_Click(sender As Object, e As EventArgs)
        MultiView1.ActiveViewIndex = 0
    End Sub

    Protected Sub grdView0_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As EXP_Get_Join_Request_Result = e.Row.DataItem
            If data.Status.Trim = "REQUEST" Then
                Dim lb As LinkButton = e.Row.FindControl("lnkApprove")
                lb.Visible = True
            End If
            'If data.Status.Length > 4 AndAlso (data.Status.Substring(0, 4) = "APPR" or ")Then

            'End If
        End If
    End Sub

    Protected Sub grdView0_RowCommand(sender As Object, e As GridViewCommandEventArgs)
        MultiView1.ActiveViewIndex = 1

        hdfReqID.Value = e.CommandArgument
        LoadView2(Convert.ToInt32(e.CommandArgument))

        If e.CommandName = "Approve" Then
            btnSave.Visible = True
            ddlStatus.Enabled = True
            ddlStatus.Items.Clear()
            ddlStatus.Items.Add(New ListItem("Approve", "APPROVED"))
            ddlStatus.Items.Add(New ListItem("Reject", "REJECT"))

        ElseIf e.CommandName = "Detail" Then
            btnSave.Visible = False
            ddlStatus.Enabled = False
        End If
    End Sub

    Private Sub LoadView2(ByVal BookID As Integer)

        Dim rqCtx As New User_RequestJoin_Service()
        Dim res As EXP_Get_Join_Request_Result = rqCtx.Search(BookID, Nothing, Nothing, Nothing, Nothing).FirstOrDefault
        txtJoinDateView1.Text = res.JoinDate.Value.ToString("dd/MM/yyyy")
        lblExpireDate.Text = res.ExpireDate.Value.ToString("dd/MM/yyyy")


        Dim usrCtx As New User_Service()
        Dim usr As REM_User = usrCtx.GetByID(res.Requester_ID)
        lblFName.Text = usr.FirstName
        lblLName.Text = usr.LastName
        lblHomePhone.Text = usr.HomePhone
        lblMobile.Text = usr.MobilePhone
        lblEmail.Text = usr.Email
        lblAddress.Text = usr.Address
        lblCreditCard.Text = usr.CreditcardNumber

        If res.Status.ToUpper() <> "REQUEST" AndAlso res.Status IsNot Nothing AndAlso res.Status <> "" Then
            ddlStatus.SelectedValue = res.Status
        Else
            ddlStatus.Items.Clear()
            ddlStatus.Items.Add("REQUEST")
        End If

    End Sub

    Protected Sub ddlStatus_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim b As Boolean = (ddlStatus.SelectedValue = "Reject")
        divReject.Visible = b
        rqRejectReason.Enabled = b
    End Sub

    Protected Sub btnSearchView0_Click(sender As Object, e As EventArgs) Handles btnSearchView0.Click
        LoadData()
        BindControl()
    End Sub

    Protected Sub txtJoinDateView1_TextChanged(sender As Object, e As EventArgs)
        Dim jd As DateTime = GetDataUtil.GetNullableDateTime(txtJoinDateView1.Text)
        lblExpireDate.Text = jd.AddDays(365).ToString("dd/MM/yyyy")
    End Sub

    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btn_search.Click
        LoadData()
        BindControl()
    End Sub

    Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btn_Clear.Click
        txtRequestor.Text = ""
        txtCheckinDateView0.Text = ""
        txtCheckoutDateView0.Text = ""
        ddl_StatusSearch.Text = ""
        LoadData()
        BindControl()
    End Sub
End Class